package com.qigao.mall.web.controller.app.merchant;


import com.qigao.mall.api.SysDictService;
import com.qigao.mall.commons.dto.SysDictDto;
import com.qigao.mall.commons.dto.SysDictResultDto;
import com.qigao.mall.commons.enums.DictTypeEnum;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.enums.StatusEnum;
import com.qigao.mall.web.controller.AbstractDictController;
import com.qigao.mall.web.vo.app.SelfSettingVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:luiz
 * @Date: 2018/2/2 10:01
 * @Descripton:
 * @Modify :
 **/
@RestController
@RequestMapping("/app/merchant/sysDict/")
@Api(tags = "App商户端自定义设置")
public class MerchantDictController extends AbstractDictController {
    private static final Logger logger = LoggerFactory.getLogger(MerchantDictController.class);
    @Resource
    private SysDictService sysDictService;

    //1.保存订单区间、运费；工作时间
    @PostMapping("/selfSetting")
    @ApiOperation("保存自定义设置 比如订单区间、运费、工作时间等")
    public Result saveSelfSetting(@RequestBody SelfSettingVo selfSettingVo){
        try{
            SysDictDto sysDictDto=new SysDictDto();
            sysDictDto.setPdictNo(DictTypeEnum.SELF_SETTING.getCode());
            sysDictDto.setDictNo(DictTypeEnum.ORDER_OVER_VAL.getCode());
            sysDictDto.setDictName(DictTypeEnum.ORDER_OVER_VAL.getName());
            sysDictDto.setDictVal(selfSettingVo.getMinOrderAmt());
            sysDictDto.setDictStatus(StatusEnum.VALID.getCode()+"");
            sysDictDto.setDictDesc(DictTypeEnum.ORDER_OVER_VAL.getName());
            sysDictService.save(sysDictDto);

            SysDictDto sysDictDto1=new SysDictDto();
            sysDictDto1.setPdictNo(DictTypeEnum.SELF_SETTING.getCode());
            sysDictDto1.setDictNo(DictTypeEnum.SHIPPING_AMT.getCode());
            sysDictDto1.setDictName(DictTypeEnum.SHIPPING_AMT.getName());
            sysDictDto1.setDictVal(selfSettingVo.getShippingAmt());
            sysDictDto1.setDictStatus(StatusEnum.VALID.getCode()+"");
            sysDictDto1.setDictDesc(DictTypeEnum.SHIPPING_AMT.getName());
            sysDictService.save(sysDictDto1);

            SysDictDto sysDictDto2=new SysDictDto();
            sysDictDto2.setPdictNo(DictTypeEnum.SELF_SETTING.getCode());
            sysDictDto2.setDictNo(DictTypeEnum.WORK_START_TIME.getCode());
            sysDictDto2.setDictName(DictTypeEnum.WORK_START_TIME.getName());
            sysDictDto2.setDictVal(selfSettingVo.getStartWorkTime());
            sysDictDto2.setDictStatus(StatusEnum.VALID.getCode()+"");
            sysDictDto2.setDictDesc(DictTypeEnum.WORK_START_TIME.getName());
            sysDictService.save(sysDictDto2);


            SysDictDto sysDictDto3=new SysDictDto();
            sysDictDto3.setPdictNo(DictTypeEnum.SELF_SETTING.getCode());
            sysDictDto3.setDictNo(DictTypeEnum.WORK_END_TIME.getCode());
            sysDictDto3.setDictName(DictTypeEnum.WORK_END_TIME.getName());
            sysDictDto3.setDictVal(selfSettingVo.getEndWorkTime());
            sysDictDto3.setDictStatus(StatusEnum.VALID.getCode()+"");
            sysDictDto3.setDictDesc(DictTypeEnum.WORK_END_TIME.getName());
            sysDictService.save(sysDictDto3);

            return Result.newSuccess();
        }catch (Exception e){
            logger.error("商户自定义设置失败:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    //1.保存订单区间、运费；工作时间
    @GetMapping("/findSelfSetting")
    @ApiOperation("查询自定义设置 比如订单区间、运费、工作时间等")
    public Result findSelfSetting(){
        try{
            Result<List<SysDictResultDto>> dictResult=sysDictService.findByPDict(DictTypeEnum.SELF_SETTING.getCode());
            if(dictResult.isSuccess()){
                List<SysDictResultDto> sysDictResultDtos=dictResult.getData();
                SelfSettingVo selfSettingVo=new SelfSettingVo();
                for(SysDictResultDto sysDictResultDto:sysDictResultDtos){
                    if(sysDictResultDto.getDictNo().equals(DictTypeEnum.WORK_START_TIME.getCode())){
                        selfSettingVo.setStartWorkTime(sysDictResultDto.getDictVal());
                    } else if(sysDictResultDto.getDictNo().equals(DictTypeEnum.WORK_END_TIME.getCode())){
                        selfSettingVo.setEndWorkTime(sysDictResultDto.getDictVal());
                    } else if(sysDictResultDto.getDictNo().equals(DictTypeEnum.ORDER_OVER_VAL.getCode())){
                        selfSettingVo.setMinOrderAmt(sysDictResultDto.getDictVal());
                    } else if(sysDictResultDto.getDictNo().equals(DictTypeEnum.SHIPPING_AMT.getCode())){
                        selfSettingVo.setShippingAmt(sysDictResultDto.getDictVal());
                    }
                }
                return Result.newSuccess(selfSettingVo);
            }
            return dictResult;
        }catch (Exception e){
            logger.error("商户自定义设置失败:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

}
